<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';

      run(async function () {
        const ponyfill = lolex.createClock();
        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill });

        WebChat.renderWebChat(
          {
            directLine,
            ponyfill,
            sendTypingIndicator: true,
            store,
            styleOptions: {
              typingAnimationBackgroundImage: `url('')`
            }
          },
          document.querySelector('main')
        );

        await pageConditions.webChatRendered();

        // Wait for "Connecting..." message to dismiss
        ponyfill.tick(600);

        await pageConditions.uiConnected();

        // WHEN: Initially.
        // THEN: Typing indicator should not be shown.
        expect(pageElements.typingIndicator()).toBeFalsy();

        // WHEN: After sending "typing 1" to the bot.
        await (await directLine.emulateOutgoingActivity('typing 1')).resolveAll();

        // WHEN: The bot send a message activity.
        await directLine.emulateIncomingActivity({
          from: { id: 'bot', role: 'bot' },
          id: crypto.randomUUID(),
          text: 'Typing indicator should go away after 5 seconds.',
          type: 'message'
        });

        // WHEN: The bot send a typing activity.
        await directLine.emulateIncomingActivity({
          from: { id: 'bot', role: 'bot' },
          id: crypto.randomUUID(),
          type: 'typing'
        });

        // THEN: Bot should send a typing indicator and shown on the screen.
        await waitFor(() => expect(pageElements.typingIndicator()).toBeTruthy());

        await host.snapshot('local');
      });
    </script>
  </body>
</html>
